package GUI;

import gameEngine.Game;
import gameEngine.GameFinder;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import connector.Controler;

@SuppressWarnings("serial")
public class PokerBoard extends JPanel implements ActionListener{
	private HoldemPanel cardsPane;
	private ControlPane controlPane;
	private PromptPanel promptPane;
	private final int WIDTH = 1000, HEIGHT = 700;
	private Player owner;
	private Controler tableControler;
	private ArrayList<PokerTable> tables;
	private boolean simulatedPlayers = false;
	
	public PokerBoard(){
		super();
		this.setSize(WIDTH, HEIGHT);
		this.owner = new Player("First-Comer", 2000, true);
		PokerTable players = new PokerTable(this, null);
		this.tables = new ArrayList<PokerTable>();
		tables.add(players);
		//this.refreshGames();
		this.layoutComponents();
		this.createTable(false);
	}
	private void refreshGames(){
		for(Game game:GameFinder.findGames()){
			if(!tables.contains(game))
					tables.add(new PokerTable(this, game));
		}
	}
	private void layoutComponents(){
		this.setLayout(new BorderLayout());
		controlPane = new ControlPane(tables.get(0), WIDTH, HEIGHT * 1/4);
		promptPane = new PromptPanel(this, WIDTH, HEIGHT * 1/4);
		cardsPane = new HoldemPanel(controlPane, WIDTH, HEIGHT * 3/4);
		this.add(cardsPane, BorderLayout.CENTER);
		this.add(promptPane, BorderLayout.PAGE_END);
		}
	private Game createNewGame(){
		return new Game("aragon", owner.getName()+"\'s table");
	}
	private void createTable(boolean start){
		PokerTable players = new PokerTable(this, createNewGame());
		tables.add(players);
		if(simulatedPlayers){
			simulateTable(players);
		}
		controlPane = new ControlPane(players, WIDTH, HEIGHT * 1/4);
		cardsPane = new HoldemPanel(controlPane, WIDTH, HEIGHT * 3/4);
		if(start)this.simulateGame();
	}
	private void simulateTable(PokerTable players){
		String[] names = {"Mary", "John", "Paul", "Peter", "Monica", "Jessica", "Brutus"};
		Random rand = new Random();
		for(int i=0; i < names.length; i++){
			players.add(new Player(names[rand.nextInt(names.length)], 200, false));
		}
	}
	private void simulateGame(){
		this.removeAll();
		this.add(cardsPane, BorderLayout.CENTER);
		this.add(controlPane, BorderLayout.PAGE_END);
		controlPane.repaint();
		this.revalidate();
		tableControler = new Controler(cardsPane, 0);
		tableControler.start();
	}
	private boolean promptAutoPlayers(){
		String[] options = {"Yes", "No"};
		int choice;
		choice = JOptionPane.showOptionDialog(this, "Would you like virtual players instead of real ones?", "Table setup options",
		JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
		return choice == 0;
	}
	private void prepareNewTable(){
		String name = (String)JOptionPane.showInputDialog(this,"Please enter your name");
		while(name != null){
			if(name.isEmpty()){
				name = (String)JOptionPane.showInputDialog(this,"Name must not be empty. \nPlease enter your name");
			}else{
				this.owner = new Player(name, 200, true);
				simulatedPlayers= promptAutoPlayers();
				this.createTable(true);
				break;
			}
		}
	}
	private void removeOwnerFromTable(){
		tableControler.removePlayer(owner);
		this.removeAll();
		tableControler.interrupt();
		cardsPane = new HoldemPanel(controlPane, WIDTH, HEIGHT * 3/4);
		this.add(cardsPane, BorderLayout.CENTER);
		promptPane = new PromptPanel(this, WIDTH, HEIGHT * 1/4);
		this.add(promptPane, BorderLayout.PAGE_END);
		this.revalidate();
	}
	public Player getOwner(){
		return this.owner;
	}
	public void joinTable(PokerTable table){
		if(!table.joinTable(owner)){
			JOptionPane.showMessageDialog(this, "There are no active games on the selected table!");
			return;
		}
		if(!tableControler.isAlive()){
			simulateGame();
		}
	}
	public ArrayList<PokerTable> getTables(){
		return tables;
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		String com = arg0.getActionCommand();
		if(com.equals(Controler.NEW_TABLE)){
			prepareNewTable();
		}else if(com.equals(Controler.LEAVE_TABLE)){
			removeOwnerFromTable();
		}
	}	
}
